package com.innovation.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

import java.net.InetSocketAddress;

import org.apache.log4j.Logger;

import com.innovation.action.ActionSet;
import com.innovation.client.ClientSet;
import com.innovation.cmd.CommandSet;
import com.innovation.config.ConfigHelper;
import com.innovation.config.GlobalConfig;
import com.innovation.config.MessageConfig;
import com.innovation.config.SqlMapperConfig;
import com.innovation.config.GlobalConfig.ZoneConfig;
import com.innovation.dao.BaseDao;
import com.innovation.thread.ScanThread;

public class GameServer {

	private static final Logger log = Logger.getLogger(GameServer.class);
	public static ZoneConfig ZONE = null;
	
	
	private static void initNetty() throws Exception{
		EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        	
        //初始化websocket服务器
        ServerBootstrap websocketBootstrap = new ServerBootstrap();
        websocketBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new WebSocketServerInitializer());
        websocketBootstrap.bind(new InetSocketAddress(ZONE.host, ZONE.websocketPort)).sync().channel();
        System.out.println("Web socket server started at host="+ZONE.host+" port=" + ZONE.websocketPort + ".");
        
        //初始化socket服务器,用于服务器之间数据的传输
        ServerBootstrap socketBootstrap = new ServerBootstrap();
        socketBootstrap.group(new NioEventLoopGroup(), new NioEventLoopGroup()).channel(NioServerSocketChannel.class).childHandler(new SocketServerInitializer());
        socketBootstrap.option(ChannelOption.TCP_NODELAY, true);
        socketBootstrap.option(ChannelOption.SO_SNDBUF, 5120);
        socketBootstrap.option(ChannelOption.SO_RCVBUF, 5120);
        socketBootstrap.bind(new InetSocketAddress(ZONE.host, ZONE.socketPort)).sync().channel();
        
        System.out.println("Socket server started at host="+ZONE.host+" port=" + ZONE.socketPort + ".");
	
        //初始化管理接口服务器(HTTP服务器)
        ServerBootstrap adminBootstrap = new ServerBootstrap();
        adminBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new AdminServerInitializer());
        adminBootstrap.bind(new InetSocketAddress(ZONE.host, ZONE.adminPort)).sync().channel();
        System.out.println("http server started at host="+ZONE.host+" port=" + ZONE.adminPort + ".");
        
	}
	
	private static void initThread(){
		//扫描线程启动
		ScanThread.INSTANCE.start();
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if(args == null || args.length < 1){
			System.out.println("please set zone id.");
			return;
		}
		
		try{
			
			long startTime = System.currentTimeMillis();
			int zoneId = Integer.valueOf(args[0]);
			GlobalConfig.init();
			ZONE = GlobalConfig.getZoneConfig(zoneId);
			
			//提示信息初始化
			MessageConfig.init();
			
			//初始化资源库
			SqlMapperConfig.initResSessionFactory();
			ConfigHelper.init();
			
			//初始化所有的操作
			ActionSet.INSTANCE.init();
			CommandSet.INSTANCE.init();
			
			//初始化mongodb
			BaseDao.initMongo();
			
			//初始化网络
			initNetty();
			
			//初始化客户端连接
			ClientSet.INSTANCE.initFightServerClient();
			
			//初始化线程
			initThread();
			
			log.info("start game server succ:zoneId="+zoneId + " useTime="+(System.currentTimeMillis() - startTime));
		}catch (Exception e) {
			e.printStackTrace();
			System.exit(0);
		}
	}
}